package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsalgorithm.spec.specConst;
import com.tc.dispatch.gsservice.basic.common.tcdate;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.tools;

public class orderInput extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orderInput() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	
	


	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		//response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
	      String action=request.getParameter("action");
	      GSServiceLocal gss=gsHelper.getGSServiceLocal(); 
	      HttpSession sess = request.getSession();
	      
	      sess.removeAttribute("position");//移除位置。
	      if(action.equals("list"))
	      {
	    	  
	    	  String dateFrom=request.getParameter("dateFrom");
	    	  String dateTo=request.getParameter("dateTo");
	          Date dateF;
	          Date dateT;
	          String spec=request.getParameter("spec");
	          String ssign=request.getParameter("ssign");
	          String plate=request.getParameter("plate");
	          if(spec==null)
	        	  spec="";
	          if(ssign==null)
	        	  ssign="";
	          if(plate==null)
	        	  plate="";
	          if(dateFrom!=null&&dateFrom.equals("null")&&sess.getAttribute("dateFrom")!=null)
	        	  dateFrom=(String)sess.getAttribute("dateFrom");
	          if(dateTo!=null&&dateTo.equals("null")&&sess.getAttribute("dateTo")!=null)
	        	  dateTo=(String)sess.getAttribute("dateTo");
	          System.out.println("dateFrom:"+dateFrom+"\ndateTo:"+dateTo);
	          if(dateFrom==null||dateFrom.equals(""))
	        	  dateF=Calendar.getInstance().getTime();
	          else
	        	  dateF=tools.stringToDate(dateFrom);
	          if(dateTo==null||dateTo.equals(""))
	        	  dateT=Calendar.getInstance().getTime();
	          else
	        	  dateT=tools.stringToDate(dateTo);
	          
	          dateF=tcdate.getStartFromDate(dateF);
	          dateT=tcdate.getEndFromDate(dateT);
	          
	          String start=request.getParameter("start");
	          String limit=request.getParameter("limit");
	          
	          String isNew=request.getParameter("isNew");
	         if(isNew==null)
	        	 isNew="";
	          List ret=new ArrayList();
	          if(isNew.equals("true"))
	          {
	
	            try{
	              ret =gss.getFinishedPro(ssign, spec, plate, dateF, dateT);
	              sess.removeAttribute("finishedDetailList");
	              sess.setAttribute("finishedDetailList", ret);
	              
	            }
	            catch(GSException e)
	            {
	            	System.out.println("GSException:"+e.getMessage());
	              //response.sendRedirect("orderError.jsp?error="+e.getErrorCode());
	            	out.print("{success:false, data:'false'}");
	            }
	          }
	          else
	          {
	        	 ret=(List)sess.getAttribute("finishedDetailList");
	          }
	         
	          int startIndex=Integer.parseInt(start);
	          int endIndex=(Integer.parseInt(start)+Integer.parseInt(limit));
	         
	          if(endIndex>ret.size())
	        	  endIndex=ret.size();
	          JSONObject json = new JSONObject();
              JsonConfig cfg = new JsonConfig();
              cfg.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
              cfg.registerJsonValueProcessor(java.sql.Date.class, new DateJsonValueProcessor());


             // BeanUtils.copyProperties();  
              json.put("totalProperty", new Integer(ret.size()));
              ret=ret.subList(startIndex, endIndex);
              JSONArray arr = JSONArray.fromObject(ret.toArray(),cfg);
              json.put("root", arr);
              response.getWriter().print(json);
              
              
	      }
	      else if(action.equals("jump"))
	      {
	    	  String dateFrom=request.getParameter("dateFrom");
	    	  String dateTo=request.getParameter("dateTo");
	          Date dateF;
	          Date dateT;
	          String spec=request.getParameter("spec");
	          String ssign=request.getParameter("ssign");
	          String plate=request.getParameter("plate");
	          if(spec==null)
	        	  spec="";
	          if(ssign==null)
	        	  ssign="";
	          if(plate==null)
	        	  plate="";
	          if(dateFrom==null||dateFrom.equals(""))
	        	  dateF=Calendar.getInstance().getTime();
	          else
	        	  dateF=tools.stringToDate(dateFrom);
	          if(dateTo==null||dateTo.equals(""))
	        	  dateT=Calendar.getInstance().getTime();
	          else
	        	  dateT=tools.stringToDate(dateTo);
	          dateF=tcdate.getStartFromDate(dateF);
	          dateT=tcdate.getEndFromDate(dateT);
	          List ret=new ArrayList();
	          try{
	              ret =gss.getFinishedPro(ssign, spec, plate, dateF, dateT);
	              sess.removeAttribute("finishedDetailList");
	              sess.setAttribute("finishedDetailList", ret);
	              
	            }
	            catch(GSException e)
	            {
	            	System.out.println("GSException:"+e.getMessage());
	              //response.sendRedirect("orderError.jsp?error="+e.getErrorCode());
	            	out.print("{success:false, data:'false'}");
	            }
	            
	            JSONObject json = new JSONObject();
	              JsonConfig cfg = new JsonConfig();
	              cfg.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
	              cfg.registerJsonValueProcessor(java.sql.Date.class, new DateJsonValueProcessor());


	             // BeanUtils.copyProperties();  
	              json.put("totalProperty", new Integer(ret.size()));
	              JSONArray arr = JSONArray.fromObject(ret.toArray(),cfg);
	              json.put("root", arr);
	              response.getWriter().print(json);
	    	  
	      }
		out.flush();
		out.close();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
